package goods.controller;

import common.model.goods.Comment;
import common.model.result.Result;
import goods.service.ICommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/comments")
public class CommentController {

    @Autowired
    private ICommentService commentService;

    /**
     * 添加评论
     * @param comment 评论信息
     * @return 结果
     */
    @PostMapping("/add")
    public Result addComment(@RequestHeader("uid") Long userId, @RequestBody Comment comment) {
        comment.setUserId(userId);
        return commentService.addComment(comment);
    }

    /**
     * 根据商品ID查询评论列表
     * @param spuId 商品ID
     * @param page 页码
     * @param pageSize 每页大小
     * @return 评论列表
     */
    @GetMapping("/spu/{spuId}")
    public Result getCommentsBySpuId(
            @PathVariable Long spuId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        return commentService.getCommentsBySpuId(spuId, page, pageSize);
    }

    /**
     * 根据用户ID查询评论列表
     * @param page 页码
     * @param pageSize 每页大小
     * @return 评论列表
     */
    @GetMapping("/user")
    public Result getCommentsByUserId(
            @RequestHeader("uid") Long userId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        return commentService.getCommentsByUserId(userId, page, pageSize);
    }

    /**
     * 更新评论状态
     * @param id 评论ID
     * @param status 状态
     * @return 结果
     */
    @PostMapping("/status/{id}")
    public Result updateCommentStatus(
            @PathVariable Long id,
            @RequestParam Integer status) {
        return commentService.updateCommentStatus(id, status);
    }

    /**
     * 删除评论
     * @param id 评论ID
     * @return 结果
     */
    @PostMapping("/delete/{id}")
    public Result deleteComment(@PathVariable Long id) {
        return commentService.deleteComment(id);
    }
} 